package cn.com.Servlet.VipApplication;

import cn.com.controler.VipType.VipTypeDAO;
import cn.com.controler.Vipapplication.VipapplicationDAO;
import cn.com.entity.User;
import cn.com.entity.Vipapplication;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "VipApplicationServlet",value = "/VipTypeCZ")
public class VipApplicationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String type = request.getParameter("type");
        VipapplicationDAO dataDAO = new VipapplicationDAO();
        if(type.equals("tj")){           //提交vip申请
            String username = request.getParameter("username");
            List<Vipapplication> allsq = dataDAO.allname(username);
            if(allsq.size() > 0)
            {
                response.getWriter().write("0");
            }else if(allsq.size() == 0){
                HttpSession session = request.getSession();
                User user = (User) session.getAttribute("user");
                Integer userId = user.getUserId();
                String password = request.getParameter("password");
                String useremail = request.getParameter("useremail");
                String zctime = request.getParameter("zctime");
                String kstime = request.getParameter("kstime");
                String jstime = request.getParameter("jstime");
                String cont = request.getParameter("cont");
                Vipapplication vipapplication = new Vipapplication(1,userId,username,password,useremail,zctime,kstime,jstime,cont,0);
                int i = dataDAO.addVipSq(vipapplication);
                if(i >0){
                    response.getWriter().write("1");
                }else{
                    response.getWriter().write("2");
                }

            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if(type.equals("cxviptype")){   //查询vip类型
            VipTypeDAO vipTypeDAO = new VipTypeDAO();
            List list = vipTypeDAO.selectVipType();
            HttpSession session = request.getSession();
            session.setAttribute("list",list);
            response.sendRedirect("/WebContent/HTML/UserPage/user_application/IntoVip.jsp");
        }else if(type.equals("cxappliaction")){      //查询申请进度
            VipapplicationDAO vipDAO = new VipapplicationDAO();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            String username = user.getUsername();
            List<Vipapplication> myapp = vipDAO.allnamee(username);
            request.setAttribute("myapp",myapp);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_application/jdselect.jsp").forward(request,response);
        }else if(type.equals("cxmyappliaction")){      //查询我的vip
            VipapplicationDAO vipDAO = new VipapplicationDAO();
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            String username = user.getUsername();
            List<Vipapplication> myapp = vipDAO.senamestate(username);
            request.setAttribute("myappp",myapp);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_application/MyVip.jsp").forward(request,response);
        }

    }
}
